package cn.vastbase.jdbc.batch;

import org.junit.Test;

import java.sql.*;

public class FetchTestCase {

    /* 定义连接对象 */
    static Connection conn = null;
    static String cname = "org.postgresql.Driver";
    static String url = "jdbc:postgresql://192.168.56.12:6000/test?reWriteBatchedInserts=true&batchMode=off";
    static String username = "zxs";
    static String passwd = "Vastdata#0730";
    /* 定义 SQL 语句执行对象 */
    static Statement stmt = null;
    static PreparedStatement pstmt = null;
    /* 定义结果集对象 */
    static ResultSet rs = null;

    public Connection conn() {
        try {
            Class.forName(cname);
            return conn = DriverManager.getConnection(url, username, passwd);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

    public void close() {
        try {
            /* 关闭结果集对象 */
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close resultSet fail." + e.getMessage());
        }
        try {
            /* 关闭语句对象 */
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close statement fail." + e.getMessage());
        }
        try {
            /* 关闭语句对象 */
            if (pstmt != null)
                pstmt.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close statement fail." + e.getMessage());
        }
        try {
            /* 关闭连接 */
            conn.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close connection fail." + e.getMessage());
        }
    }

    /**
     * @Title: 分批查询
     * @Description:
     * total=10, fetchSize=2
     * 数据库日志 log_statement='all'
     * 2024-10-17 09:02:52.589 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  0 [BACKEND] LOG:  libpqsw(47608372918304-0):we find pbe new select cmdtag:START TRANSACTION, sql:START TRANSACTION
     * 2024-10-17 09:02:52.589 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  0 [BACKEND] LOG:  execute S_1: START TRANSACTION
     * 2024-10-17 09:02:52.590 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  0 [BACKEND] LOG:  libpqsw(47608372918304-0):we find pbe new select cmdtag:SELECT, sql:SELECT * FROM users
     * 2024-10-17 09:02:52.590 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  844424930247943 [BACKEND] LOG:  execute S_2/C_3: SELECT * FROM users
     * 2024-10-17 09:02:52.671 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  0 [BACKEND] LOG:  execute fetch from S_2/C_3: SELECT * FROM users
     * 2024-10-17 09:02:52.739 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  0 [BACKEND] LOG:  execute fetch from S_2/C_3: SELECT * FROM users
     * 2024-10-17 09:02:52.781 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  0 [BACKEND] LOG:  execute fetch from S_2/C_3: SELECT * FROM users
     * 2024-10-17 09:02:52.831 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  0 [BACKEND] LOG:  execute fetch from S_2/C_3: SELECT * FROM users
     * 2024-10-17 09:02:52.881 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  0 [BACKEND] LOG:  execute fetch from S_2/C_3: SELECT * FROM users
     * 2024-10-17 09:02:52.940 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  0 [BACKEND] LOG:  libpqsw(47608372918304-0):we find pbe new select cmdtag:COMMIT, sql:COMMIT
     * 2024-10-17 09:02:52.940 zxs zxsdb 172.17.1.101 47608364009216 0[0:0#0]  0 [BACKEND] LOG:  execute S_4: COMMIT
     */
    @Test
    public void testFetch() {

        if (conn() == null)
            return;
        try {
            conn.setAutoCommit(false);
            Statement st = conn.createStatement();
            // 打开游标，每次获取50行数据
            st.setFetchSize(2);
            ResultSet rs = st.executeQuery("SELECT * FROM users");
            int i = 0;
            while (rs.next()) {
                System.out.println((++i) + ":a row was returned.");
            }
            conn.commit();
            rs.close();

            // 关闭服务器游标。一次查询
            st.setFetchSize(0);
            rs = st.executeQuery("SELECT * FROM users");
            int j = 0;
            while (rs.next()) {
                System.out.println((++j) + "：many rows were returned.");
            }
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }
}
